package com.py.pythink.third;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import com.py.pythink.config.SystemConfig;
import com.py.pythink.util.DataUtil;

import java.io.InputStream;
import java.util.Date;

public class AliOssUtil {

    private static final String accessKeyId="LTAI5t8QA19CRR2V15oHyHW8";//
    private static final String accessKeySecret="OqUKmRFUnAYm0frzUc43uKnk5BNoaP";//
    public static final String bucket="data213";
    private static final String endpoint="oss-cn-beijing.aliyuncs.com";
    private static OSS client;
    static {
        client=new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }
    /**
     * 上传文件*/
    public static String upload(String objName, InputStream is){
        //上传文件到云存储中心
        client.putObject(bucket,objName,is);
        //生成访问地址
        return createUrl(objName);
    }
    /**
     * 上传文件*/
    public static String upload(String objName, Date date, InputStream is){
        //上传文件到云存储中心
        client.putObject(bucket,objName,is);
        //生成访问地址
        return createUrl(objName,date);
    }
    /**
     * 验证对象名称是否存在*/
    public static boolean checkName(String objName){
        return client.doesObjectExist(bucket,objName);
    }
    /**
     * 删除文件*/
    public static boolean delObject(String objName){
        if(client.doesObjectExist(bucket,objName)){
            //sha
            client.deleteObject(bucket,objName);
            return true;
        }
        return false;
    }
    public static String createUrl(String objName){
        return client.generatePresignedUrl(bucket,objName, DataUtil.getDate((SystemConfig.OSS_URL_DAYS))).toString();
    }
    public static String createUrl(String objName, Date date){
        return client.generatePresignedUrl(bucket,objName, date).toString();
    }
}
